# -*- coding: utf-8 -*-
# created on 2016/6/27

from sympy import S, Poly
from sympy.abc import x
from mathsolver.functions.base import BaseFunction, BaseEq, BaseIneq, base_gen, BaseEqs, BaseIneqs, new_latex
from mathsolver.functions.fangcheng.yici import YiCi005
from mathsolver.functions.sympy_utils import safe_degree, default_symbol


# 判断一次函数图像经过/不经过的象限
class HanShuYiCiXiangXian(BaseFunction):
    def solver(self, *args):
        f2 = args[0].expression
        text = args[1]
        symbol = default_symbol(f2)
        assert len(f2.free_symbols) == 1 and safe_degree(f2, x) == 1

        f2_poly = Poly(f2.as_expr(), symbol)
        a, b = f2_poly.all_coeffs()

        all_ = {"一", "三", "二", "四"}
        if a > 0:
            xiangxian = ["一", "三"]
            if b > 0:
                xiangxian.append("二")
            elif b < 0:
                xiangxian.append("四")
        elif a < 0:
            xiangxian = ["二", "四"]
            if b > 0:
                xiangxian.append("一")
            elif b < 0:
                xiangxian.append("三")
        else:
            raise ValueError

        if "不" in text:
            self.steps.append(["", "函数不经过%s象限" % "、".join(all_.difference(xiangxian))])
            self.label.add("判断一次函数图像不经过的象限")
        else:
            self.steps.append(["", "函数经过%s象限" % "、".join(xiangxian)])
            self.label.add("判断一次函数图像经过的象限")
        return self


# 根据正比例函数的定义求参
class HanShuYiCiZhenBiLi(BaseFunction):
    def solver(self, *args):
        f2 = args[0].expression
        try:
            res = YiCi005().solver(BaseEq([f2, S.Zero])).output[0]
            if isinstance(res, BaseEq) or isinstance(res, BaseIneq):
                eqs = [res.value]
            elif isinstance(res, BaseEqs) or isinstance(res, BaseIneqs):
                eqs = res.value
        except Exception:
            eqs = []

        if f2.is_Add:
            a, _ = f2.as_independent(x)
            eqs.append([a, S.Zero])

        if len(eqs) > 1:
            self.steps.append(["由正比例函数的定义得", self.output_eqs(eqs)])
        else:
            self.steps.append(["由正比例函数的定义得", self.output_eq(eqs[0])])

        self.label.add("根据正比例函数的定义求参")
        self.output.append(base_gen(eqs))
        return self


# 函数的平移变换问题
class HanShuPingYi(BaseFunction):
    def solver(self, *args):
        f1, f2 = args[0].sympify()
        symbol = default_symbol(f2)
        number = args[1].sympify()
        text = args[2]
        if safe_degree(f2, symbol) == 1:
            self.label.add("一次函数的平移变换")
            f2_poly = Poly(f2.as_expr(), symbol)
            a, b = f2_poly.all_coeffs()
            if "向上" in text:
                self.steps.append(["", "平移函数得到"])
                new_f2 = f2 + number
                self.steps.append(["", "%s = %s + %s" % (new_latex(f1), new_latex(f2), new_latex(number))])
                self.steps.append(["", "整理，得 %s = %s" % (new_latex(f1), new_latex(new_f2))])
            elif "向下" in text:
                self.steps.append(["", "平移函数得到"])
                new_f2 = f2 - number
                self.steps.append(["", "%s = %s - %s" % (new_latex(f1), new_latex(f2), new_latex(number))])
                self.steps.append(["", "整理，得 %s = %s" % (new_latex(f1), new_latex(new_f2))])
            elif "向左" in text:
                self.steps.append(["", "平移函数得到"])
                new_x = symbol + number
                new_f2 = a * new_x + b
                if a == 1:
                    self.steps.append(["", "%s = %s + %s"
                                       % (new_latex(f1), new_latex(new_x), new_latex(b))])
                else:
                    self.steps.append(["", "%s = %s * (%s) + %s"
                                       % (new_latex(f1), new_latex(a), new_latex(new_x), new_latex(b))])
                self.steps.append(["", "整理，得 %s = %s" % (new_latex(f1), new_latex(new_f2))])
            elif "向右" in text:
                self.steps.append(["", "平移函数得到"])
                new_x = symbol - number
                new_f2 = a * new_x + b
                if a == 1:
                    self.steps.append(["", "%s = %s + %s"
                                       % (new_latex(f1), new_latex(new_x), new_latex(b))])
                else:
                    self.steps.append(["", "%s = %s * (%s) + %s"
                                       % (new_latex(f1), new_latex(a), new_latex(new_x), new_latex(b))])
                self.steps.append(["", "整理，得 %s = %s" % (new_latex(f1), new_latex(new_f2))])
        elif safe_degree(f2, symbol) == 2:
            self.label.add("二次函数的平移变换")
            f2_poly = Poly(f2.as_expr(), symbol)
            a, b, c = f2_poly.all_coeffs()
            if "向上" in text:
                self.steps.append(["", "平移函数得到"])
                new_f2 = f2 + number
                self.steps.append(["", "%s = %s + %s" % (new_latex(f1), new_latex(f2), new_latex(number))])
                self.steps.append(["", "整理，得 %s = %s" % (new_latex(f1), new_latex(new_f2))])
            elif "向下" in text:
                self.steps.append(["", "平移函数得到"])
                new_f2 = f2 - number
                self.steps.append(["", "%s = %s - %s" % (new_latex(f1), new_latex(f2), new_latex(number))])
                self.steps.append(["", "整理，得 %s = %s" % (new_latex(f1), new_latex(new_f2))])
            elif "向左" in text:
                constant1 = b / (2 * a)
                constant2 = (4 * a * c - b ** 2) / (4 * a)
                f2_dingdian = a * (symbol + constant1) ** 2 + constant2
                self.steps.append(["", "将二次函数化为顶点式，得 %s = %s" % (new_latex(f1), new_latex(f2_dingdian))])
                new_x = symbol + number
                self.steps.append(["", "平移函数得到"])
                new_f2 = a * (new_x + constant1) ** 2 + constant2
                if a == 1:
                    if constant1 > 0:
                        self.steps.append(["", "%s = ( %s + %s )**2 + %s"
                                           % (new_latex(f1), new_latex(new_x), new_latex(constant1), new_latex(constant2))])
                        self.steps.append(["", "整理，得 %s = %s" % (new_latex(f1), new_latex(new_f2))])
                    else:
                        self.steps.append(["", "%s = ( %s %s )**2 + %s"
                                           % (new_latex(f1), new_latex(new_x), new_latex(constant1), new_latex(constant2))])
                        self.steps.append(["", "整理，得 %s = %s" % (new_latex(f1), new_latex(new_f2))])
                else:
                    if constant1 > 0:
                        self.steps.append(["", "%s = %s * ( %s + %s )**2 + %s"
                                           % (new_latex(f1), new_latex(a), new_latex(new_x), new_latex(constant1), new_latex(constant2))])
                        self.steps.append(["", "整理，得 %s = %s" % (new_latex(f1), new_latex(new_f2))])
                    else:
                        self.steps.append(["", "%s = %s * ( %s %s )**2 + %s"
                                           % (new_latex(f1), new_latex(a), new_latex(new_x), new_latex(constant1), new_latex(constant2))])
                        self.steps.append(["", "整理，得 %s = %s" % (new_latex(f1), new_latex(new_f2))])
            elif "向右" in text:
                constant1 = b / (2 * a)
                constant2 = (4 * a * c - b ** 2) / (4 * a)
                f2_dingdian = a * (x + constant1) ** 2 + constant2
                self.steps.append(["", "将二次函数化为顶点式，得 %s = %s" % (new_latex(f1), new_latex(f2_dingdian))])
                new_x = symbol - number
                self.steps.append(["", "平移函数得到"])
                new_f2 = a * (new_x + constant1) ** 2 + constant2
                if a == 1:
                    if constant1 > 0:
                        self.steps.append(["", "%s = ( %s + %s )**2 + %s"
                                           % (new_latex(f1), new_latex(new_x), new_latex(constant1), new_latex(constant2))])
                        self.steps.append(["", "整理，得 %s = %s" % (new_latex(f1), new_latex(new_f2))])
                    else:
                        self.steps.append(["", "%s = ( %s %s )**2 + %s"
                                           % (new_latex(f1), new_latex(new_x), new_latex(constant1), new_latex(constant2))])
                        self.steps.append(["", "整理，得 %s = %s" % (new_latex(f1), new_latex(new_f2))])
                else:
                    if constant1 > 0:
                        self.steps.append(["", "%s = %s * ( %s + %s )**2 + %s"
                                           % (new_latex(f1), new_latex(a), new_latex(new_x), new_latex(constant1), new_latex(constant2))])
                        self.steps.append(["", "整理，得 %s = %s" % (new_latex(f1), new_latex(new_f2))])
                    else:
                        self.steps.append(["", "%s = %s * ( %s %s )**2 + %s"
                                           % (new_latex(f1), new_latex(a), new_latex(new_x), new_latex(constant1), new_latex(constant2))])
                        self.steps.append(["", "整理，得 %s = %s" % (new_latex(f1), new_latex(new_f2))])
        return self


# 根据正比例函数所在的象限求参
class HanShuZhengBiLiOnXiangXian(BaseFunction):
    def solver(self, *args):
        f2 = args[0].expression
        symbol = args[0].var
        text = args[1]
        k, h = f2.as_independent(symbol)
        assert h == symbol
        if ("一" and "三") in text:
            ineq = [k, ">", 0]
            self.steps.append(["依题意，得", BaseIneq(ineq).printing()])
        elif ("二" and "四") in text:
            ineq = [k, "<", 0]
            self.steps.append(["依题意，得", BaseIneq(ineq).printing()])
        else:
            raise ValueError
        self.output.append(BaseIneq(ineq))
        self.label.add("根据正比例函数所在的象限求参")
        return self


if __name__ == '__main__':
    pass
